// @ts-nocheck
const pool = require('../mysql/mysql.js')
const { validateFields } = require('../public/util.js')
const { successMsg, errorMsg } = require('../public/constant.js')
const {executeTransaction}=require('../mysql/public.js')
const express = require('express');
const router = express.Router();
const multer = require('multer');
const upload = multer();

const tableName='role'
// 查询数据

router.get('/List', (req, res) => {
    pool.query(`SELECT * FROM ${tableName} Where is_delete=0`, (err, result) => {
        if (err){
            return res.status(500).send(errorMsg('数据库错误'));
        }
        var r = [...result]
        res.status(200).send(successMsg(r));
    });
});

// 添加
router.post('/Add', upload.none(), validateFields([
    { field: 'roleName', noEmpty: true, validType: 'string' },
]), (req, res) => {
    const { roleName } = req.body;
    var roleId=''
    executeTransaction({pool,res,queries:[
        {
            sql:`select * from ${tableName} where role_name=? and is_delete=0`,
            params:[roleName],
            stopFlag:(result)=>result.length,
            stopEvent:()=>{res.status(200).send(errorMsg('名称重复'))},
            successEvent:()=>{}
        },
        {
            sql:`SELECT role_id FROM ${tableName} ORDER BY id DESC LIMIT 1`,
            params:[],
        },
        {
            sql:`INSERT INTO ${tableName} (role_id,role_name) VALUES (?,?)`,
            params:[roleId, roleName],
            modifyParamsEvent:(params,result)=>{
                var lastValue = result[0] ? result[0].role_id : 'role_0';
                var lastNumber = parseInt(lastValue.split('_')[1]);
                params[0]= 'role_' + (lastNumber + 1).toString();
                return params
            },
            successEvent:()=>{res.status(200).send(successMsg());}
        },
    ]})
    
});
// 删除
router.get('/Delete', (req, res) => {
    const { roleId } = req.query
    if (!roleId) {
        return res.status(200).send(errorMsg('缺少参数roleId'));
    }
    executeTransaction({pool,res,queries:[
        {
            sql:`SELECT * FROM ${tableName} WHERE role_id=? and is_delete=0`,
            params:[roleId],
            stopFlag:(result)=>!result.length,
            stopEvent:()=>{res.status(200).send(errorMsg('无效id'))}
        },
        {
            sql:`UPDATE ${tableName} SET is_delete=1 WHERE role_id=?`,
            params:[roleId],
            successEvent:()=>{res.status(200).send(successMsg('操作成功'));}
        }
    ]})
});
// 获取某一个角色
router.post('/GetOne', upload.none(), validateFields([
    { field: 'roleId', noEmpty: true, validType: 'string' },
]), (req, res) => {
    const { roleId } = req.body
    const sql = `SELECT * FROM ${tableName} WHERE role_id=? and is_delete=0`
    pool.query(sql, [roleId], (err, result) => {
        if (err){
            return res.status(500).send(errorMsg('数据库错误'));
        }
        var r = [...result]
        res.status(200).send(successMsg(r))
    })
});
// 修改角色
router.post('/Edit', upload.none(), validateFields([
    { field: 'roleId', noEmpty: true, validType: 'string' },
    { field: 'roleName', noEmpty: true, validType: 'string' },
]), (req, res) => {
    const { roleId, roleName } = req.body;
    const sql = `SELECT * FROM ${tableName} WHERE role_id=? and is_delete=0`
    executeTransaction({pool,res,queries:[
        {
            sql:`SELECT * FROM ${tableName} WHERE role_id=? and is_delete=0`,
            params:[roleId],
            stopFlag:(result)=>!result.length,
            stopEvent:()=>{res.status(200).send(errorMsg('没有该角色'))}
        },
        {
            sql:`SELECT * FROM ${tableName} WHERE role_name=? and is_delete=0`,
            params:[roleName],
            stopFlag:(result)=>result.length,
            stopEvent:()=>{res.status(200).send(errorMsg('名称重复'))}
        },
        {
            sql:`UPDATE ${tableName} SET role_name=? WHERE role_id=?`,
            params:[roleName, roleId],
            successEvent:()=>{res.status(200).send(successMsg('修改成功'))}
        }
    ]})
});

module.exports = router;